01 / 05

Describe how you would implement a thread-safe LRU cache in Go for a high-throughput service.

Combine a doubly linked list for O(1) eviction ordering with a hashmap for O(1) lookup, protected by a sync.RWMutex. For extreme throughput, use sharded locks or a library like ristretto.

Thread-safe LRU cache implementation
Scaling for high throughput
  1. 1

    Sharded locks: partition key space into N buckets, each with its own mutex — reduces contention

  2. 2

    sync.Map: suitable for read-heavy workloads with infrequent writes

  3. 3

    ristretto (DGraph): production-grade cache with TTL, admission policy, and metrics

  4. 4

    groupcache: distributed caching for multi-node setups

  5. 5

    Always benchmark with realistic concurrency before choosing between mutex and sync.Map